import re

import requests

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

options = Options()
options.add_argument("--headless")

headers_iPhone = {
            'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 15_4_1 like Mac OS X)'
                     'AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.4 Mobile/15E148 Safari/604.1'
        }



def RenewCookies ():
    rid = "1962143378"
    url = "https://live.douyin.com/" + str(rid)
    browser = webdriver.Firefox(options=options)
    browser.get(str(url))
    cookie = browser.execute_script("return document.cookie")
    browser.close()
    return cookie

def RequestRoomID ( web_rid , cookie ):
    cookie = str(cookie)
    headers_PC = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0',
        'Cookie': str(cookie)
}
    url = "https://live.douyin.com/" + str(web_rid)
    request = requests.get(url, headers=headers_PC).text
    roomId = re.search(r'roomId%22%3A%22(\d+)%22%2C%22', request).group(1)
    return str(roomId)


def RequestInformation( id,room_id ):
    url = "https://webcast.amemv.com/webcast/room/reflow/info/?type_id=0&live_id=1&room_id=" + str(room_id) + "&app_id=1128"
    response = requests.get(url=url, headers=headers_iPhone).text
    if id == "nickname":
        return re.search(r'"nickname":"(.*?)"', response).group(1)
    if id == "signature":
        return re.search(r'"signature":"(.*?)"', response).group(1)
    if id == "rtmp_pull_url":
        return re.search(r'"rtmp_pull_url":"(.*?flv)"', response).group(1)
    if id == "hls_pull_url":
        return re.search(r'"hls_pull_url":"(.*?m3u8)"', response).group(1)
    if id == "title":
        return re.search(r'"title":"(.*?)"', response).group(1)
